home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Sound / PreludeAMP / src / huffman.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-25  |  4.4 KB  |  211 lines

  1.  
  2. /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
  3. */
  4.  
  5. /* huffman.c  huffman decoding
  6.  *
  7.  * Created by: tomislav uzelac  Mar,Apr 1996
  8.  * Last modified by: tomislav uzelac Mar  8 97
  9.  */
  10. #include "amiga.h"
  11. #include "audio.h"
  12. #include "getbits.h"
  13.  
  14. #define HUFFMAN
  15. #include "huffman.h"
  16.  
  17. static inline unsigned int viewbits(int n)
  18. {
  19. unsigned int pos,ret_value;
  20.  
  21.         pos = data >> 3;
  22.         ret_value = buffer[pos] << 24 |
  23.                     buffer[pos+1] << 16 |
  24.                     buffer[pos+2] << 8 |
  25.                     buffer[pos+3];
  26.         ret_value <<= data & 7;
  27.         ret_value >>= 32 - n;
  28.  
  29.         return ret_value;
  30. }
  31.  
  32. static inline void sackbits(int n)
  33. {
  34.         data += n;
  35.         data &= 8*BUFFER_SIZE-1;
  36. }
  37.  
  38. /* huffman_decode() is supposed to be faster now
  39.  * decodes one codeword and returns no. of bits
  40.  */
  41. static inline int huffman_decode(int tbl,int *x,int *y)
  42. {
  43. unsigned int chunk;
  44. register unsigned int *h_tab;
  45. register unsigned int lag;
  46. register unsigned int half_lag;
  47. int len;
  48.  
  49.     h_tab=tables[tbl];
  50.     chunk=viewbits(19);
  51.  
  52.     h_tab += h_cue[tbl][chunk >> (19-NC_O)];
  53.  
  54.     len=(*h_tab>>8)&0x1f;
  55.  
  56.     /* check for an immediate hit, so we can decode those short codes very fast
  57.     */
  58.     if ((*h_tab>>(32-len)) != (chunk>>(19-len))) {
  59.         if (chunk >> (19-NC_O) < N_CUE-1)
  60.           lag=(h_cue[tbl][(chunk >> (19-NC_O))+1] -
  61.                h_cue[tbl][chunk >> (19-NC_O)]);
  62.         else {
  63.             /* we strongly depend on h_cue[N_CUE-1] to point to
  64.              * the last entry in the huffman table, so we should
  65.              * not get here anyway. if it didn't, we'd have to
  66.              * have another table with huffman tables lengths, and
  67.              * it would be a mess. just in case, scream&shout.
  68.              */ 
  69.             printf(" h_cue clobbered. this is a bug. blip.\n");
  70.             exit (-1);
  71.         }
  72.         chunk <<= 32-19;
  73.         chunk |= 0x1ff;
  74.  
  75.         half_lag = lag >> 1;
  76.  
  77.         h_tab += half_lag;
  78.         lag -= half_lag;
  79.  
  80.         while (lag > 1) {
  81.                 half_lag = lag >> 1;
  82.  
  83.                 if (*h_tab < chunk)
  84.                         h_tab += half_lag;
  85.                 else
  86.                         h_tab -= half_lag;
  87.  
  88.                         lag -= half_lag;
  89.         }
  90.  
  91.         len=(*h_tab>>8)&0x1f;
  92.         if ((*h_tab>>(32-len)) != (chunk>>(32-len))) {
  93.                 if (*h_tab > chunk)
  94.                         h_tab--;
  95.                 else 
  96.                         h_tab++;
  97.           
  98.                 len=(*h_tab>>8)&0x1f;
  99.         }
  100.     }
  101.     sackbits(len);
  102.     *x=(*h_tab>>4)&0xf;
  103.     *y=*h_tab&0xf;
  104.     return len;
  105. }
  106.  
  107. static inline int _qsign(int x,int *q)
  108. {
  109. int ret_value=0,i;
  110.     for (i=3;i>=0;i--) 
  111.         if ((x>>i) & 1) {
  112.             if (getbits(1)) *q++=-1;
  113.                 else *q++=1;
  114.             ret_value++;
  115.         }
  116.         else *q++=0;
  117.     return ret_value;
  118. }        
  119.  
  120. int decode_huffman_data(struct SIDE_INFO *info,int gr,int ch,int ssize)
  121. {
  122. int l,i,cnt,x,y;
  123. int q[4],r[3],linbits[3],tr[4]={0,0,0,0};
  124. int big_value = info->big_values[gr][ch] << 1;
  125.  
  126.     for (l=0;l<3;l++) {
  127.         tr[l]=info->table_select[gr][ch][l];
  128.         linbits[l]=t_linbits[info->table_select[gr][ch][l]];
  129.     }
  130.  
  131.     tr[3]=32+info->count1table_select[gr][ch];
  132.  
  133.     /* we have to be careful here because big_values are not necessarily
  134.      * aligned with sfb boundaries
  135.      */
  136.     if (!info->window_switching_flag[gr][ch] && info->block_type[gr][ch]==0) {
  137.  
  138.     /* this code needed some cleanup
  139.     */
  140.         r[0]=t_l[info->region0_count[gr][ch]] + 1;
  141.         if (r[0] > big_value)
  142.             r[0]=r[1]=big_value;
  143.         else {
  144.             r[1]=t_l[ info->region0_count[gr][ch] + info->region1_count[gr][ch] + 1 ] + 1;
  145.             if (r[1] > big_value)
  146.                 r[1]=big_value;
  147.         }
  148.         r[2]=big_value;
  149.  
  150.     } else {
  151.  
  152.         if (info->block_type[gr][ch]==2 && info->mixed_block_flag[gr][ch]==0) 
  153.             r[0]=3*(t_s[2]+1);
  154.         else 
  155.             r[0]=t_l[7]+1;
  156.  
  157.         if (r[0] > big_value)
  158.             r[0]=big_value;
  159.  
  160.         r[1]=r[2]=big_value;
  161.     }
  162.  
  163.     l=0; cnt=0;
  164.     for (i=0;i<3;i++) {
  165.         for (;l<r[i];l+=2) {
  166.                 int j = linbits[i];
  167.  
  168.             cnt+=huffman_decode(tr[i],&x,&y);
  169.  
  170.             if (x==15 && j>0) {
  171.                     x+=getbits(j);
  172.                     cnt+=j;
  173.             }
  174.             if (x) {
  175.                     if (getbits(1)) x=-x;
  176.                     cnt++;
  177.             }
  178.             if (y==15 && j>0) {
  179.                     y+=getbits(j);
  180.                     cnt+=j;
  181.             }
  182.             if (y) {
  183.                     if (getbits(1)) y=-y;
  184.                     cnt++;
  185.             }
  186.  
  187.             is[ch][l]=x;
  188.             is[ch][l+1]=y;
  189.         }
  190.     }
  191.     while ((cnt < info->part2_3_length[gr][ch]-ssize) && (l<576)) {
  192.         cnt+=huffman_decode(tr[3],&x,&y);
  193.         cnt+=_qsign(x,q);
  194.         for (i=0;i<4;i++) is[ch][l+i]=q[i]; /* ziher je ziher, is[578]*/
  195.         l+=4;
  196.     }
  197.  
  198.     /*  set position to start of the next gr/ch
  199.      */
  200.      if (cnt != info->part2_3_length[gr][ch] - ssize ) {
  201.          data-=cnt-(info->part2_3_length[gr][ch] - ssize);
  202.          data&= 8*BUFFER_SIZE - 1;
  203.      }
  204.     if (l<576) non_zero[ch]=l;
  205.     else non_zero[ch]=576;
  206.     /* zero out everything else
  207.     */
  208.     for (;l<576;l++) is[ch][l]=0;
  209.     return 1;
  210. }
  211.